# JavaScript Array sort() Method

JavaScript Array Reference

## Example

Sort an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();

The result of fruits will be:

Apple,Banana,Mango,Orange
Try it yourself »

More "Try it Yourself" examples below.

## Definition and Usage

The sort() method sorts the items of an array.

The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down).

By default, the sort() method sorts the values as strings in alphabetical and ascending order.

This works well for strings ("Apple" comes before "Banana"). However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".

Because of this, the sort() method will produce an incorrect result when sorting numbers.

You can fix this by providing a "compare function" (See "Parameter Values" below).

Note: This method changes the original array.

## Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method |  |  |  |  |
--- | --- | --- | --- | --- | ---
sort() | 1.0 | 5.5 | 1.0 | Yes | Yes

## Syntax

array.sort(compareFunction)

## Parameter Values

Parameter | Description
--- | ---

compareFunction

Optional. A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like:

- function(a, b){return a-b}

When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

Example:

When comparing 40 and 100, the sort() method calls the compare function(40,100).

The function calculates 40-100, and returns -60 (a negative value).

The sort function will sort 40 as a value lower than 100.

## Technical Details

Return Value: | The Array object, with the items sorted
JavaScript Version: | 1.1

## More Examples

## Example

Sort numbers in an array in ascending order:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});

The result of points will be:

1,5,10,25,40,100
Try it yourself »

## Example

Sort numbers in an array in descending order:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});

The result of points will be:

100,40,25,10,5,1
Try it yourself »

## Example

Get the highest value in an array:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a}); // Sort the numbers in the array in descending order
// The first item in the array (points[0]) is now the highest value

The result of points[0] will be:

100
Try it yourself »

## Example

Get the lowest value in an array:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b}); // Sort the numbers in the array in ascending order
// The first item in the array (points[0]) is now the lowest value

The result of points[0] will be:

1
Try it yourself »

## Example

Sort an array alphabetically, and then reverse the order of the sorted items (descending):

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();

The result of fruits will be:

Orange,Mango,Banana,Apple
Try it yourself »

JavaScript Array Reference

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples